package com.test.sdk.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author 徒有琴
 */
public class TicketUtil {
    private static SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

    public static String encode(String ticket) {
        Random random = new Random();
        int n = random.nextInt(9) + 1;//不要0
        Date now = new Date();
        String time = format.format(now);
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < time.length(); i++) {
            builder.append(ticket.charAt(i * 2));
            builder.append(ticket.charAt(i * 2 + 1));
            builder.append(Integer.toHexString((time.charAt(i) - '0') ^ n));//转16进制
        }
        builder.append(ticket.substring(28,32));
        builder.append(n);
        return builder.toString();
    }

    public static Date decodeDate(String ticket) {
        int num = ticket.charAt(ticket.length() - 1) - '0';
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 14; i++) {
            builder.append(Integer.valueOf((ticket.charAt(i * 3 + 2) + ""), 16) ^ num);
        }

        String time = builder.toString();
        try {
            return format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static  String decode(String ticket){
        StringBuilder builder = new StringBuilder();
        for(int i=0;i<ticket.length()-5;i+=3){
            builder.append(ticket.charAt(i));
            builder.append(ticket.charAt(i+1));
        }
        builder.append(ticket.substring(42,46));
        return builder.toString();
    }

    public static void main(String[] args) {
        //d9ae35bc597e0807b9569c10bff308db
        //d9ae35bc597e0807b9569c10bff308db
        //d91ae3352bca5927e1083076b925669c710abf3f3a08d3
        //d92ae0351bc95917e2080075b915659c4109bf1f3008db
        //01 34 67 9,10
        //d94ae6357bcf5977e4086073b975639c2101bf2f326
      // System.out.println(encode("d9ae35bc597e0807b9569c10bff308db"));
       // System.out.println(decode("d95ae7356bce5967e5087072b965629c4107bf7f3e7"));
      //  System.out.println(Integer.parseInt("a", 16));
        System.out.println("d97ae5354bcc5947e7085070b945609c0105bf7f3608db5".length());
        System.out.println( decode("d97ae5354bcc5947e7085070b945609c0105bf7f3608db5"));

        System.out.println("/user/login.html".matches("/user/service/.*"));
        System.out.println("/user/service/login.html".matches("/user/service/.*"));
    }
}
